package com.yu.core.controller.user;


import java.io.File;
import java.io.IOException;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.yu.model.User;
import com.yu.util.CookiesUtil;
import com.yu.util.StringUtil;

public class UserController extends Controller {
	
	public void index(){
		setAttr("username", CookiesUtil.getName(getRequest(), "username"));
		render("login.html");
	}
	
	public void login(){
		String username = getPara("username");
		
		// 记住我
		if(StringUtil.isEmpty(getPara("remember-me"))){
			CookiesUtil.add(getResponse(), "username", username);
		}
		if ("abc".equals(getPara("username"))){
			render("main.html");
		} else {
			setAttr("username", username);
			render("login.html");
		}
		
	}
	
	
	public void main(){
		renderText("登陆成功！");
	}
	
	public void uedit(){
		render("uedit.html");
	}
	
	public void ueditSave(){
		String content = getPara("content");
		System.out.println(content);
		setAttr("content", content);
		render("uedit.html");
	}

	public void excel(){
		 // 第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("学生表一");
        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步，创建单元格，并设置值表头 设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("项目名称");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("项目描述");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("创始人信息");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("项目介绍");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("市场前景");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("竞争优势");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("团队介绍");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("上传时间");
        cell.setCellStyle(style);
        /*
        for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow((int) i + 1);
            // row.createCell((short) 0).setCellValue(product.title);
        }
        */
        // 第六步，将文件存到指定位置
        try {
            //获取网站部署路径(通过ServletContext对象)，用于确定下载文件位置，从而实现下载
            //String path = getRequest().getServletContext().getRealPath("/");
            //1.设置文件ContentType类型，这样设置，会自动判断下载文件类型
        	getResponse().setContentType("multipart/form-data");
            //2.设置文件头：最后一个参数是设置下载文件名(假如我们叫a.pdf)
        	getResponse().setHeader("Content-Disposition", "attachment;fileName="+"product.xls");
            ServletOutputStream out;
            try {
                //3.通过response获取ServletOutputStream对象(out)
                out = getResponse().getOutputStream();
                wb.write(out);
                out.close();
                out.flush();
            } catch (IOException e) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        renderNull();
	}
	@Before(UserValidator.class)
	public void save(){
		User user = getModel(User.class);
		System.out.println(user.getStr("name"));
		System.out.println(getPara("user.name"));
		renderText("OK");
	}
}
